$(function() {
	admin.deliveryUser.init();
});
Namespace('admin.deliveryUser', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				/*var row = $('#datagrid').datagrid('getSelected');
				admin.supplier.edit(row.id);*/
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	toAdd : function() {
		top.addTab('#tabs','添加配送人员', contextPath+'/send/deliveryUser/add');
	},
	edit:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		top.addTab(id,'编辑配送人员', contextPath+'/send/deliveryUser/update/'+id);
	},
	chooseParentId:function() { //选择上级配送人员
		getDialog('query-dialog').dialog({
			href:contextPath+'/send/deliveryUser/choose',
			title: '点击选择',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.deliveryUser.addParentId();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addParentId:function() { //选择社区
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var parentId = rows[0].id;
		var parentName = rows[0].userNick;
		var parentUserId = rows[0].userId;
		$('#parentId').val(parentId);
		$('#parentName').val(parentName);
		$('#parentUserId').val(parentUserId);
	},
	chooseUser:function() { //选择用户
		getDialog('query-dialog').dialog({
			href:contextPath+'/user/base/choose',
			title: '点击选择用户',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.deliveryUser.addUser();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addUser:function() {
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var userId = rows[0].id;
		var userNick = rows[0].nick;
		$('#userId').val(userId);
		$('#userNick').val(userNick);
	},
	chooseCommunity:function() { //选择社区
		getDialog('query-dialog').dialog({
			href:contextPath+'/community/base/choose',
			title: '点击选择社区',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.deliveryUser.addCommunity();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addCommunity:function() { //选择社区
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var communityId = rows[0].id;
		var communityName = rows[0].name;
		$('#communityId').val(communityId);
		$('#communityName').val(communityName);
	},
	setAuditStatus: function (id,auditStatus) {
		var msg = "确定要操作吗";
		if (auditStatus == 2) {
			msg = "确定审核通过吗";
		} else if (auditStatus == 3) {
			msg = "确定审核不通过吗";
		}
		$.messager.confirm('信息提示', msg, function (success) {
			if (!success) {
				return;
			}
			var url = contextPath + '/send/deliveryUser/setAuditStatus';
			$.postJson(url, {'id': id,'auditStatus':auditStatus}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					admin.deliveryUser.query();
				} else {
					top.showInfo(result.msg);
				}
			});
		});
	},
	setStatus: function (id,status) {
		var msg = "确定要操作吗";
		if (status == 1) {
			msg = "确定开启吗";
		} else if (status == 2) {
			msg = "确定暂停吗";
		}
		$.messager.confirm('信息提示', msg, function (success) {
			if (!success) {
				return;
			}
			var url = contextPath + '/send/deliveryUser/setStatus';
			$.postJson(url, {'id': id,'status':status}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					admin.deliveryUser.query();
				} else {
					top.showInfo(result.msg);
				}
			});
		});
	},
});